<!--
 * @Author: dlfan
 * @Date: 2019-12-10 10:54:12
 * @LastEditors: dlfan
 * @LastEditTime: 2019-12-10 11:06:25
 * @Description:
 -->
<template>
  <div id="map">
  </div>
</template>

<script>
  import AMap from "AMap";
  export default {
    data() {
      return {};
    },
    // 钩子函数
    mounted() {
      this.initMap()
    },
    methods: {
      // 初始化地图，即刚开始时显示的地图图层的样子
      initMap() {
        // 设置三个参数
        var map = new AMap.Map("map", {
          center: [115.681, 34.727], // [纬度，经度]
          resizeEnable: true,
          zoom: 5
        });
        AMap.plugin(["AMap.ToolBar", "AMap.Scale"], function() {
          map.addControl(new AMap.ToolBar());
          map.addControl(new AMap.Scale());
          // 设置样式id
          map.setMapStyle('amap://styles/d96e77b3943717f47d5a072f570b8805');
        });

        // 添加标记并实现信息窗体显示事件
        /*
            方法一：
            var infoWindow = new AMap.InfoWindow({ //创建信息窗体
              isCustom: true,  //使用自定义窗体
              content:'<div>信息窗体</div>', //信息窗体的内容可以是任意html片段
              offset: new AMap.Pixel(16, -45)
            });
            var onMarkerClick  =  function(e) {
              infoWindow.open(map, e.target.getPosition());//打开信息窗体
              //e.target就是被点击的Marker
            }
            var marker = new AMap.Marker({
              position: [116.481181, 39.989792]
            })
            map.add(marker);
            marker.on('click',onMarkerClick);//绑定click事件
        */

        // 方法二：
        //添加多个点的位置坐标
        /*var lnglats = [
          [115.9013, 35.78872],
          [115.2301, 36.38149],
          [116.05026, 35.61666],
          [117.45483, 39.91816]
        ];*/

        //循环添加marker标记  并添加事件
/*
        for (var i = 0, marker; i < lnglats.length; i++) {
          var marker = new AMap.Marker({
            position: lnglats[i],
            map: map
          });
          //实例化信息窗体
          var  content = [];
          content.push("<img src='http://www.sdmz.net/d/map/sh/shren.png'>宋江");
          content.push("星号：天魁星");
          content.push("星号：天魁星");
          var infoWindow = new AMap.InfoWindow({
            isCustom: true,  //使用自定义窗体
            content: createInfoWindow(title, content.join("<br/>")),
            offset: new AMap.Pixel(16, -45)
          });

          marker.on('click', markerClick);
          marker.emit('click', {target: marker});
        }

        //构建自定义信息窗体
        function createInfoWindow(title, content) {
          var info = document.createElement("div");
          info.className = "custom-info input-card content-window-card";

          //可以通过下面的方式修改自定义窗体的宽高
          //info.style.width = "400px";
          // 定义顶部标题
          var top = document.createElement("div");
          var titleD = document.createElement("div");
          var closeX = document.createElement("img");
          top.className = "info-top";
          titleD.innerHTML = title;
          closeX.src = "https://webapi.amap.com/images/close2.gif";
          closeX.onclick = closeInfoWindow;

          top.appendChild(titleD);
          top.appendChild(closeX);
          info.appendChild(top);

          // 定义中部内容
          var middle = document.createElement("div");
          middle.className = "info-middle";
          middle.style.backgroundColor = 'white';
          middle.innerHTML = content;
          info.appendChild(middle);

          // 定义底部内容
          var bottom = document.createElement("div");
          bottom.className = "info-bottom";
          bottom.style.position = 'relative';
          bottom.style.top = '0px';
          bottom.style.margin = '0 auto';
          var sharp = document.createElement("img");
          sharp.src = "https://webapi.amap.com/images/sharp.png";
          bottom.appendChild(sharp);
          info.appendChild(bottom);
          return info;
        }

        function markerClick(e) {
          infoWindow.setContent(e.target.content);
          infoWindow.open(map, e.target.getPosition());
        }
        // map.setFitView(); //适应当前显示
        */

        /*var lnglats = [
          [115.9013, 35.78872],
          [115.2301, 36.38149],
          [116.05026, 35.61666],
          [117.45483, 39.91816]
        ];*/
        addMarker();
        // for (var i = 0; i < lnglats.length; i++) {
          function addMarker() {
            map.clearMap();
            var marker = new AMap.Marker({
              map: map,
              position: [115.9013, 35.78872]
            });
            //鼠标点击marker弹出自定义的信息窗体
            AMap.event.addListener(marker, 'click', function () {
              infoWindow.open(map, marker.getPosition());
            });
          }
        // }


        //实例化信息窗体
        var title = '大兄弟<span style="font-size:11px;color:#F00;">108好汉</span>',
          content = [];
        content.push("<img src='http://www.sdmz.net/d/map/sh/shren.png'>宋江");
        content.push("星号：天魁星");
        content.push("星号：天魁星");
        var infoWindow = new AMap.InfoWindow({
          isCustom: true,  //使用自定义窗体
          content: createInfoWindow(title, content.join("<br/>")),
          offset: new AMap.Pixel(16, -45)
        });

        //构建自定义信息窗体
        function createInfoWindow(title, content) {
          var info = document.createElement("div");
          info.className = "custom-info input-card content-window-card";

          //可以通过下面的方式修改自定义窗体的宽高
          info.style.width = "150px";
          // 定义顶部标题
          var top = document.createElement("div");
          var titleD = document.createElement("div");
          var closeX = document.createElement("img");
          top.className = "info-top";
          titleD.innerHTML = title;
          closeX.src = "https://webapi.amap.com/images/close2.gif";
          closeX.onclick = closeInfoWindow;

          top.appendChild(titleD);
          top.appendChild(closeX);
          info.appendChild(top);

          // 定义中部内容
          var middle = document.createElement("div");
          middle.className = "info-middle";
          middle.style.backgroundColor = 'white';
          middle.innerHTML = content;
          info.appendChild(middle);

          // 定义底部内容
          var bottom = document.createElement("div");
          bottom.className = "info-bottom";
          bottom.style.position = 'relative';
          bottom.style.top = '0px';
          bottom.style.margin = '0 auto';
          var sharp = document.createElement("img");
          sharp.src = "https://webapi.amap.com/images/sharp.png";
          bottom.appendChild(sharp);
          info.appendChild(bottom);
          return info;
        }


          //关闭信息窗体
          function closeInfoWindow() {
            map.clearInfoWindow();
          }


      },

    }
  };
</script>
<style>
  #map {
    height: 100%;
  }

  /*信息窗体样式*/
   html, body, #container {
     height: 100%;
     width: 100%;
   }

  .content-window-card {
    position: relative;
    box-shadow: none;
    bottom: 0;
    left: 0;
    width: auto;
    padding: 0;
  }

  .content-window-card p {
    height: 2rem;
  }

  .custom-info {
    border: solid 1px silver;
  }

  div.info-top {
    position: relative;
    background: none repeat scroll 0 0 #F9F9F9;
    border-bottom: 1px solid #CCC;
    border-radius: 5px 5px 0 0;
  }

  div.info-top div {
    display: inline-block;
    color: #333333;
    font-size: 14px;
    font-weight: bold;
    line-height: 31px;
    padding: 0 10px;
  }

  div.info-top img {
    position: absolute;
    top: 10px;
    right: 10px;
    transition-duration: 0.25s;
  }

  div.info-top img:hover {
    box-shadow: 0px 0px 5px #000;
  }

  div.info-middle {
    font-size: 12px;
    padding: 10px 6px;
    line-height: 20px;
  }

  div.info-bottom {
    height: 0px;
    width: 100%;
    clear: both;
    text-align: center;
  }

  div.info-bottom img {
    position: relative;
    z-index: 104;
  }

  span {
    margin-left: 5px;
    font-size: 11px;
  }

  .info-middle img {
    float: left;
    margin-right: 6px;
  }
</style>

